package org.jsets.fastboot.eventbus;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class RetryTask implements Delayed {

	private final long delay;
	private final long expire;
	private final int step;
	private final Object event;
	private final String subscriberKey;

	public RetryTask(Object event, String subscriberKey, long delay, int step) {
		this.event = event;
		this.subscriberKey = subscriberKey;
		this.delay = delay;
		this.step = step;
		this.expire  = System.currentTimeMillis() + (delay*1000);
	}

	@Override
	public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
	}

	@Override
	public long getDelay(TimeUnit unit) {
		return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
	}
	
	@Override
	public String toString() {
		return "RetryTask delay["+this.delay+"]";
	}

	public long getExpire() {
		return expire;
	}

	public Object getEvent() {
		return event;
	}

	public String getSubscriberKey() {
		return subscriberKey;
	}

	public int getStep() {
		return step;
	}

	public long getDelay() {
		return delay;
	}
}